package com.haoshui.controller;


import com.haoshui.domain.ResponseResult;
import com.haoshui.enums.AppHttpCodeEnum;
import com.haoshui.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class uploadController {
    @Autowired
    private UploadService uploadService;


    /**
     * 上传文章图片
     * @param img
     * @return
     */
    @PostMapping("/upload")
    public ResponseResult uploadImg(@RequestParam("img") MultipartFile img){
        try {
            System.out.println(img.getSize());
            long size = img.getSize();
            if(size>=900000L){
                return ResponseResult.errorResult(AppHttpCodeEnum.IMG_OVER_LIMIT);
            }else{
                return uploadService.uploadImg(img);
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("上传图片失败");
        }
    }
}
